class MyNumbers:

    def __init__(self,numbers):
        self.numbers = numbers

    """python中使用obj[]获取元素时，会调用__getitem__方法"""
    def __getitem__(self, item):
        if isinstance(item,slice):
            # 如果是切片操作,则返回与原本数据类型一致的数据
            return MyNumbers(self.numbers[item])
        return self.numbers[item]


    def __setitem__(self, key, value):
        # 在这里可以做一些限制,比如果要求元素都是数字,如果不是就报错
        if not isinstance(value,(int,float)):
            raise ValueError('value must be int')
        self.numbers[key] = value

    def __delitem__(self, key):
        # 不允许删完
        if len(self.numbers) == 1:
            raise ValueError('cannot delete last item')
        self.numbers.pop(key)

    # in 操作的时候会调用该方法
    def __contains__(self, item):
        return item in self.numbers
    def __str__(self):
        return str(self.numbers)

a_list = [1,2,3,4,5]
a_dict = {'a':1,'b':2,'c':3}
my_numbers = MyNumbers(a_list)
# print(my_numbers['a'])

print(type(my_numbers[1:3]))
print(my_numbers[1:3])
